package com.wandian.audio.tools;

import android.util.Log;

import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.bean.MusicBean;
import com.wandian.audio.bean.MusicTopRankListBean;
import com.wandian.audio.bean.SingerSongEntityBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MusicCollector {
    public static int song_total_number = 0;
    private static List<MusicBean> mySongs = new ArrayList<>();//歌曲数据
    private static List<MusicBean> songsList = new ArrayList<>();//顺序播放
    private static List<Integer> randomListIndex = new ArrayList<>();//随机播放下标顺序

    public static List<Integer> getRandomListIndex() {
        return randomListIndex;
    }

    public static void setRandomListIndex(List<Integer> randomListIndex) {
        MusicCollector.randomListIndex = randomListIndex;
    }

    public static void clearAllSong(){

        mySongs.clear();
        song_total_number = mySongs.size();
        Log.d("clearAllSong",mySongs.size()+"");
    }
    /**
     * 获取给定index的song*/
    public static MusicBean getSong(int index){
        return mySongs.get(index);
    }
    /**
     * 向songs中添加Song对象*/
    public static void addSong(MusicBean song){
        mySongs.add(song);
        song_total_number++;
    }
    /**
     * 从songs中移除Song对象*/
    public static void removeSong(int index){
        mySongs.remove(index);
        song_total_number--;
    }
    /**
     * 从songs中移除Song对象*/
    public static int getSongIndexByID(String musicID){
        if(!UIUtils.isEmpty(mySongs) && mySongs.size()>0){
            for(int i =0;i<mySongs.size();i++){
                if(mySongs.get(i).getId().equals(musicID)){
                    return i;
                }
            }
        }
        return 0;
    }
    /**
     * 从songs中移除Song对象*/
    public static int getSongIndexByLocal(String musicPath){
        if(!UIUtils.isEmpty(mySongs) && mySongs.size()>0){
            for(int i =0;i<mySongs.size();i++){
                if(mySongs.get(i).getUrl().equals(musicPath)){
                    return i;
                }
            }
        }
        return 0;
    }
    //重载
    public static void removeSong(String dataPath){
        for (int i=0; i < mySongs.size(); i++) {
            MusicBean s = mySongs.get(i);
            if (s.getUrl().equals(dataPath)) {
                mySongs.remove(i);
                song_total_number--;
                break;
            }
        }
    }

    /**
     * 获取song数量*/
    public static int size(){
        return song_total_number;
    }
    /**
     * 返回songs_list对象*/
    public static List<MusicBean> getSongsList(){
        return mySongs;
    }
    /**
     * 设置songs_list对象*/
    public static void setSongsList(List<MusicBean> songs){
        mySongs = songs;
        song_total_number = songs.size();
        songsList = songs;

    }

    /**
     * 设置MusicEntityBean_list对象*/
    public static void setToSongsList(List<SingerSongEntityBean> songs, String author){

        if(!UIUtils.isEmpty(songs) && songs.size()>0){
            List<MusicBean> mySongList = new ArrayList<>();
            for(SingerSongEntityBean bean:songs){
                MusicBean musicBean = new MusicBean();
                musicBean.setId(bean.getId()+"");
                musicBean.setName(bean.getName());
                musicBean.setNickname(author);
                mySongList.add(musicBean);
            }
            mySongs = mySongList;
            song_total_number = songs.size();
            songsList = mySongList;
        }

    }

    /**
     * 设置MusicEntityBean_list对象*/
    public static void setTopRankToSongsList(List< MusicTopRankListBean.PlaylistEntity.TracksEntity> songs){

        if(!UIUtils.isEmpty(songs) && songs.size()>0){
            List<MusicBean> mySongList = new ArrayList<>();
            for( MusicTopRankListBean.PlaylistEntity.TracksEntity bean:songs){
                MusicBean musicBean = new MusicBean();
                musicBean.setId(bean.getId()+"");
                musicBean.setName(bean.getName());

                if(!UIUtils.isEmpty(bean.getAl())){
                    musicBean.setThumb(bean.getAl().getPicUrl());
                }
                if(!UIUtils.isEmpty(bean.getAr()) && bean.getAr().size()>0){
                    if(UIUtils.isEmpty(bean.getAr().get(0).getName())){
                        musicBean.setNickname( "未知");
                    }else {
                        musicBean.setNickname(bean.getAr().get(0).getName()+"");
                    }
                }
                mySongList.add(musicBean);
            }
            mySongs = mySongList;
            song_total_number = songs.size();
            songsList = mySongList;
        }

    }

    /**
     * 判断是否已经有这首歌曲*/
    public static boolean isContainSong(String dataPath){
        for (MusicBean mySong : mySongs) {
            if (mySong.getUrl().equals(dataPath)) {
                return true;
            }
        }
        return false;
    }

    public static int getSongIndex(MusicBean song){
       int result = 0;
       for(int i=0; i < mySongs.size(); i++){
           if(mySongs.get(i).getUrl().equals(song.getUrl())){
               result = i;
               break;
           }
       }
       return  result;
    }

    //打乱播放顺序
    public static void setRandomMusicIndex(){
        randomListIndex.clear();
        List<Integer> listData = new ArrayList<>();//随机顺序歌曲
        Random random = new Random();
        int maxLength = mySongs.size();
        for(int i=0;i<maxLength;i++){
            listData.add(i);
        }
        for(int i=0;i<maxLength;i++){
            int index = random.nextInt(listData.size());
            randomListIndex.add(listData.get(index));
            listData.remove(index);
        }
    }
    //获取下一首随机播放的顺序
    public static int getRandomMusicNextIndex(int playIndex){
        if(randomListIndex.size()>=mySongs.size()){
            randomListIndex.clear();
        }
        List<Integer> listData = new ArrayList<>();//随机顺序歌曲
        int maxLength = mySongs.size();
        for(int i=0;i<maxLength;i++){
            if(randomListIndex.size()>0){
                for(int k=0;k<randomListIndex.size();k++){
                    if(i != randomListIndex.get(k)){
                        listData.add(i);
                    }
                }
            }else {
                listData.add(i);
            }
        }
        Random random = new Random();
        int nextIndex = 0;
        if(listData.size()>0){
            while (playIndex ==nextIndex){
                nextIndex = random.nextInt(listData.size());
            }
        }
        randomListIndex.add(nextIndex);
        return nextIndex;

    }


}
